//
//  BIDSaleHouseDetailViewController.m
//  HJGJCommunity
//
//  Created by mal on 2017/6/16.
//  Copyright © 2017年 mal. All rights reserved.
//

#import "BIDSaleHouseDetailViewController.h"
#import "BIDRentDetailCell.h"
#import "BIDCustomForRentLabel.h"
#import <SDWebImage/UIImageView+WebCache.h>

static NSString *detailURL = @"saleAndroid/getById.action";

@interface BIDSaleHouseDetailViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
{
    NSArray *_dataSourceArr;
}
@end

@implementation BIDSaleHouseDetailViewController
@synthesize saleID;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"房屋详情";
    // Do any additional setup after loading the view from its nib.
    _dataSourceArr = [[NSArray alloc] init];
    [_collectionView registerClass:[BIDRentDetailCell class] forCellWithReuseIdentifier:@"identifier"];
    //
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [flowLayout setItemSize:CGSizeMake(screenSize.width, screenSize.width*167/320)];
    [flowLayout setMinimumLineSpacing:0];
    [flowLayout setMinimumInteritemSpacing:0];
    [_collectionView setCollectionViewLayout:flowLayout];
    _collectionView.pagingEnabled = YES;
    _collectionView.bounces = NO;
    //
    [_placeholderImgView setImage:[UIImage imageNamed:PIC_PLACEHOLDER]];
    //
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], detailURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"sale.id=%@", self.saleID];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        if(errMsg)
        {
            [self showAlertView:errMsg buttonTitle:ALERT_BTN_TITLE tag:0 functionName:nil];
        }
        else
        {
            [self prepareForLayoutWithDic:responseDic];
        }
    }];
}

- (void)prepareForLayoutWithDic:(NSDictionary *)dic
{
    NSArray *arr = dic[@"fileList"];
    [_pageCtrl setNumberOfPages:arr.count];
    _dataSourceArr = [[NSArray alloc] initWithArray:dic[@"fileList"]];
    _placeholderImgView.hidden = _dataSourceArr.count>0;
    [_collectionView reloadData];
    //
    CGFloat height = [BIDGlobalMethods getHeightWithString:dic[@"content"] font:_titleLabel.font constraintSize:CGSizeMake(CGRectGetWidth(_titleLabel.frame), MAXFLOAT)];
    if(height+20 < 44)
    {}
    else
    {
        _heightForTitleLabelConstraint.constant = height + 20;
    }
    _titleLabel.text = dic[@"content"];
    //
    _houseTypeLabel.text = dic[@"houseTypeName"];
    _areaLabel.text = [[NSString alloc] initWithFormat:@"%@㎡", dic[@"area"]];
    _floorLabel.text = [[NSString alloc] initWithFormat:@"%@楼", dic[@"floor"]];
    _kindTypeLabel.text = dic[@"propertyName"];
    _totalFloorsLabel.text = [[NSString alloc] initWithFormat:@"%@楼", dic[@"totalFloors"]];
    _priceLabel.text = [[NSString alloc] initWithFormat:@"%@万", dic[@"price"]];
    _timesLabel.text = dic[@"years"];
    _contacterLabel.text = dic[@"contactName"];
    _phoneLabel.text = dic[@"tel"];
    _decorationTypeLabel.text = dic[@"decorateName"];
    height = [BIDGlobalMethods getHeightWithString:dic[@"address"] font:_contentLabel.font constraintSize:CGSizeMake(CGRectGetWidth(_contentLabel.frame), MAXFLOAT)];
    if(height+20 < 44)
    {}
    else
    {
        _heightForContentLabelConstraint.constant = height + 20;
    }
    _contentLabel.text = dic[@"address"];
    //
    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
    _heightForContainerViewConstraint.constant = CGRectGetMaxY(_contentLabel.frame)+20;
    CGSize size = _scrollView.contentSize;
    size.height = _heightForContainerViewConstraint.constant;
    [_scrollView setContentSize:size];
}

- (IBAction)callIn:(id)sender
{
    if(_phoneLabel.text.length > 0)
    {
        NSString *str = [[NSString alloc] initWithFormat:@"tel://%@", _phoneLabel.text];
        NSURL *url = [NSURL URLWithString:str];
        UIWebView *telWebView = [[UIWebView alloc] init];
        [telWebView loadRequest:[NSURLRequest requestWithURL:url]];
        [self.view addSubview:telWebView];
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWidth = scrollView.frame.size.width;
    NSInteger page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    NSLog(@"%ld", page);
    [_pageCtrl setCurrentPage:page];
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSourceArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = nil;
    NSUInteger row = indexPath.row;
    BIDRentDetailCell *rentCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"identifier" forIndexPath:indexPath];
    NSDictionary *dic = _dataSourceArr[row];
    NSString *strImgDownloadURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], dic[@"path"]];
    [rentCell.imgView setImageWithURL:[NSURL URLWithString:strImgDownloadURL] placeholderImage:[UIImage imageNamed:SLIDE_PLACEHOLDER]];
    cell = rentCell;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
}

@end
